/**
 * 
 */
package sailing.filters;

import java.util.List;

import javax.swing.event.EventListenerList;

import sailing.filters.ui.FilterListPanelListener;
import cjlite.utils.Lists;

/**
 * @author kevin
 * 
 */
public class FilterListModel {

	/** List of listeners */
	protected EventListenerList listenerList = new EventListenerList();

	private List<Filter> filterList = Lists.newArrayList();

	public void addFilter(Filter filter) {
		filterList.add(filter);
		this.fireDataChanged();
	}

	public void removeModelListener(FilterListPanelListener filterListPanel) {
		this.listenerList.remove(FilterListPanelListener.class, filterListPanel);
	}

	public void addModelListener(FilterListPanelListener filterListPanel) {
		this.listenerList.add(FilterListPanelListener.class, filterListPanel);
	}

	/**
	 * @param e
	 * @see sailing.filters.FilterListModelEvent
	 */
	public void fireDataChanged(FilterListModelEvent e) {
		// Guaranteed to return a non-null array
		Object[] listeners = listenerList.getListenerList();
		// Process the listeners last to first, notifying
		// those that are interested in this event
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == FilterListPanelListener.class) {
				((FilterListPanelListener) listeners[i + 1]).dataChanged(e);
			}
		}
	}

	/**
	 * 
	 */
	public void fireDataChanged() {
		fireDataChanged(new FilterListModelEvent(this));
	}

	/**
	 * @return
	 */
	public List<Filter> getFilterList() {
		return this.filterList;
	}

	public void remove(Filter f) {
		this.filterList.remove(f);
		this.fireDataChanged();
	}

}
